import { Controller, Get, Param, Delete, Query, Res } from '@nestjs/common';
import { CommentsService } from './comments.service';

@Controller()
export class CommentsController {
  constructor(private readonly commentsService: CommentsService) {}

  /**
   * 获取所有评论
   *
   * @param { page } 页码
   * @param { pageSize } 页面大小
   * @param { commentNickname } 评论人昵称(模糊)
   * @param { comment } 评论(模糊)
   * @returns {{ list: Comments[],total: number }} 评论列表和数量
   */
  @Get()
  async getCommentList(@Query() query) {
    const { page, pageSize, commentNickname, comment } = query;
    const [list, total] = await this.commentsService.getCommentList(
      +page,
      +pageSize,
      commentNickname,
      comment,
    );
    return { list, total };
  }

  /**
   * 根据评论ID删除评论
   *
   * @param { commentId } 评论ID
   * @returns {{ MesResponse }}
   */
  @Delete(':commentId')
  async deleteComment(@Param('commentId') commentId: number, @Res() res) {
    await this.commentsService.deleteComment(+commentId);
    res.send({
      code: 0,
      msg: '删除成功',
    });
  }
}
